package com.ruoyi.book.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.book.domain.TbBookList;
import com.ruoyi.book.mapper.TbBookListMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.book.mapper.TbBookBorrowMapper;
import com.ruoyi.book.domain.TbBookBorrow;
import com.ruoyi.book.service.ITbBookBorrowService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-11
 */
@Service
public class TbBookBorrowServiceImpl implements ITbBookBorrowService 
{
    @Autowired
    private TbBookBorrowMapper tbBookBorrowMapper;
    @Autowired
    private TbBookListMapper tbBookListMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public TbBookBorrow selectTbBookBorrowById(Long id)
    {
        return tbBookBorrowMapper.selectTbBookBorrowById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tbBookBorrow 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TbBookBorrow> selectTbBookBorrowList(TbBookBorrow tbBookBorrow)
    {
        return tbBookBorrowMapper.selectTbBookBorrowList(tbBookBorrow);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tbBookBorrow 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbBookBorrow(TbBookBorrow tbBookBorrow)
    {
        tbBookBorrow.setCreateTime(DateUtils.getNowDate());
        TbBookList tbBookList = tbBookListMapper.selectTbBookListById(tbBookBorrow.getBookId());
        tbBookList.setBookMount(tbBookList.getBookMount() - tbBookBorrow.getBorrowMount());
        tbBookListMapper.updateTbBookList(tbBookList);
        return tbBookBorrowMapper.insertTbBookBorrow(tbBookBorrow);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tbBookBorrow 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTbBookBorrow(TbBookBorrow tbBookBorrow)
    {
        tbBookBorrow.setUpdateTime(DateUtils.getNowDate());
        return tbBookBorrowMapper.updateTbBookBorrow(tbBookBorrow);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTbBookBorrowByIds(String ids)
    {
        return tbBookBorrowMapper.deleteTbBookBorrowByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTbBookBorrowById(Long id)
    {
        return tbBookBorrowMapper.deleteTbBookBorrowById(id);
    }

    @Override
    @Transactional
    public boolean still(Long id) {
        TbBookBorrow tbBookBorrow = tbBookBorrowMapper.selectTbBookBorrowById(id);
        TbBookList tbBookList = tbBookListMapper.selectTbBookListById(tbBookBorrow.getBookId());
        tbBookList.setBookMount(tbBookList.getBookMount() + tbBookBorrow.getBorrowMount());
        tbBookListMapper.updateTbBookList(tbBookList);
        tbBookBorrow.setStill(1L);
        tbBookBorrow.setStillDate(new Date());
        tbBookBorrowMapper.updateTbBookBorrow(tbBookBorrow);
        return true;
    }

    @Override
    public TbBookBorrow selectTbBookListByBookId(Long bookId) {
        return tbBookBorrowMapper.selectTbBookListByBookId(bookId);
    }
}
